﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;
using LynxWebLog.Models;

namespace LynxWebLog.Controllers
{
    //[Authorize]
    public class NewBlogUserController : Controller
    {
        public NewBlogUserController()
            : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
        {
        }

        public NewBlogUserController(UserManager<ApplicationUser> userManager)
        {
            UserManager = userManager;
        }

        public UserManager<ApplicationUser> UserManager { get; private set; }

        private Microsoft.Owin.Security.IAuthenticationManager AuthenticationManager
        {
            get
            {
                return HttpContext.GetOwinContext().Authentication;
            }
        }
        //
        // GET: /NewBlogUser/
        public async Task<ActionResult> NewBlog(string blogName)
        {
            string userId = this.UserManager.FindByName(User.Identity.Name).Id;
            IdentityResult result = await this.UserManager.AddToRoleAsync(userId, "1");

            CookieFacade.CurrentBlog = blogName;
            BlogSupport bs = new BlogSupport();
            await bs.CreateNewBlog(User.Identity.Name, blogName, Request.RequestContext);

            return RedirectToAction("Details", "BlogData", new { id = User.Identity.Name + "_" + blogName });
        }
	}
}